package testing;

import java.util.Hashtable;
import java.util.Map;

import dse.Cluster;
import dse.DataObject;
import dse.DataObjectAccessRanking;
import dse.Network;
import dse.Node;
import dse.Pair;

public class OneRequestScheduler 
extends dse.modules.GeneralRequestEventScheduler
{
	private boolean tookFirst = false;
	private Pair<Node,DataObject> requestToMake;
	
	
	public Pair<Node,DataObject> getOnlyRequestToMake (
			Network network,
			Map<Cluster, DataObjectAccessRanking> accessRankings
			)
	{
		if (requestToMake != null)
			return requestToMake;

		while (true) {
			Map<Node, DataObject> m = 
				super.getRequests(network, accessRankings);
			
			for (Node n : m.keySet()) {
				requestToMake = new Pair<Node,DataObject>(n, m.get(n));
				return requestToMake;
			}
		}
	}
	
	
	public Map<Node, DataObject> getRequests (
			Network network,
			Map<Cluster, DataObjectAccessRanking> accessRankings
			)
	{
		if (requestToMake == null) 
			getOnlyRequestToMake(network, accessRankings);
		if (tookFirst)
			return new Hashtable<Node, DataObject>();
		tookFirst = true;
		Map<Node,DataObject> onlyOne = new Hashtable<Node, DataObject>();
		onlyOne.put(requestToMake.first, requestToMake.second);
		return onlyOne;
	}
}